package com.xxx.video.mine.presenters;

import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;


import com.xxx.video.R;
import com.xxx.video.commen.beans.BaseBean;
import com.xxx.video.commen.presenters.BasePresenter;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.commen.utils.ResponseUtils;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.commen.utils.ToastUtils;
import com.xxx.video.mine.beans.FeedBackImageBean;
import com.xxx.video.mine.beans.FeedBackTypeListBean;
import com.xxx.video.mine.beans.UploadFileBean;
import com.xxx.video.network.listeners.CommonResponseListener;
import com.xxx.video.network.utils.NetWorkUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FeedBackPresenter extends BasePresenter<IFeedBackView> {
    /**
     * upload image file
     */
    private List<FeedBackImageBean> tempFeedBackImageBeanList;

    public FeedBackPresenter(@NonNull Context context, IFeedBackView baseView) {
        super(context, baseView);
    }

    /**
     * upload feed back image list
     *
     * @param feedBackImageBeanList image list
     */
    public void uploadImage(List<FeedBackImageBean> feedBackImageBeanList) {
        if (ListUtils.isEmpty(feedBackImageBeanList)) {
            /*
             * upload image file all success
             */
            feedBack();
            return;
        }
        FeedBackImageBean feedBackImageBean = feedBackImageBeanList.get(0);
        NetWorkUtils.getInstance().uploadFile(new File(feedBackImageBean.filePath), new CommonResponseListener<UploadFileBean>() {

            @Override
            public void onSuccess(UploadFileBean successOb, String result) {
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (ListUtils.isEmpty(tempFeedBackImageBeanList)) {
                        tempFeedBackImageBeanList = new ArrayList<>();
                    }
                    feedBackImageBean.uploadUrl = successOb.url;
                }
                tempFeedBackImageBeanList.add(feedBackImageBean);
                /*
                 * next
                 */
                feedBackImageBeanList.remove(0);
                uploadImage(feedBackImageBeanList);
            }

            @Override
            public void onFailure(Object failureObj) {
                hideProgressDialog();
            }
        });

    }

    /**
     * submit feed back
     */
    public void submitFeedBack() {
        if (tempFeedBackImageBeanList != null) {
            tempFeedBackImageBeanList.clear();
        }
        showProgressDialog("loading");
        List<FeedBackImageBean> feedBackImageBeans = null;
        if (getBaseView() != null) {
            feedBackImageBeans = getBaseView().getFeedBackImageList();
        }
        if (ListUtils.isEmpty(feedBackImageBeans)) {
            feedBack();
        } else {
            uploadImage(feedBackImageBeans);
        }
    }

    /**
     * feed back
     */
    public void feedBack() {
        /*
         * feed back type
         */
        FeedBackTypeListBean.ListBean listBean = getBaseView() == null ? null : getBaseView().getFeedBackType();
        /*
         * feed back content
         */
        String feedBackContent = getBaseView() == null ? "" : getBaseView().getFeedBackContent();
        /*
         * email
         */
        String email = getBaseView() == null ? "" : getBaseView().getEmail();
        if (listBean == null) {
            hideProgressDialog();
            ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_register_first_pwd_again_not_queals));
            return;
        }
        if (TextUtils.isEmpty(feedBackContent)) {
            hideProgressDialog();
            ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_feed_back_content_can_not_empty));
            return;
        }
        /*
         * image
         */
        String imageList = "";
        if (!ListUtils.isEmpty(tempFeedBackImageBeanList)) {
            for (FeedBackImageBean feedBackImageBean : tempFeedBackImageBeanList) {
                if (!TextUtils.isEmpty(feedBackImageBean.uploadUrl)) {
                    if (TextUtils.isEmpty(imageList)) {
                        imageList = feedBackImageBean.uploadUrl;
                    } else {
                        imageList = imageList + "," + feedBackImageBean.uploadUrl;
                    }
                }

            }
        }
        NetWorkUtils.getInstance().feedBack(listBean.fDatadictId, listBean.fDatadictName, feedBackContent, email, imageList, new CommonResponseListener<BaseBean>() {
            @Override
            public void onSuccess(BaseBean successOb, String result) {
                hideProgressDialog();
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().feedBackSuccess();
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {
                hideProgressDialog();
            }
        });
    }

    /**
     * get feed back type list
     */
    public void getFeedBackTypeList() {
        NetWorkUtils.getInstance().getFeedBackTypeList(new CommonResponseListener<FeedBackTypeListBean>() {
            @Override
            public void onSuccess(FeedBackTypeListBean successOb, String result) {
                if (getBaseView() != null) {
                    getBaseView().showRootLayoutStatus(1);
                }
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().initFeedBackTypeListData2View(successOb);
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {
                if (getBaseView() != null) {
                    getBaseView().showRootLayoutStatus(1);
                }

            }
        });
    }
}
